package com.digitalchina.lxnsy.activity.management;

import android.app.DatePickerDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.FertilizerVO;
import com.digitalchina.lxnsy.entity.LandVO;
import com.digitalchina.lxnsy.toolkit.CustomDialog;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.OkHttpUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.xml.AdminXML;
import com.lzy.okhttputils.model.HttpParams;

import java.util.Calendar;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity.management
 * 创建人：郑皓
 * 创建时间：2019/4/25 17:08
 * 修改备注：
 * ================================================
 */
public class PesticideActivity extends BaseActivity {

    @Bind(R.id.tv_date)
    TextView tvDate;
    @Bind(R.id.tv_land)
    TextView tvLand;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.et_num_area)
    EditText etNumArea;
    @Bind(R.id.et_dosage)
    EditText etDosage;
    @Bind(R.id.et_member)
    EditText etMember;
    @Bind(R.id.tv_dosage)
    TextView tvDosage;

    private OptionsPickerView pvName, pvLand;
    private DatePickerDialog pvDate;

    private int mYear;
    private int mMonth;
    private int mDayOfMonth;

    private String pesticidesStandardId = ""; // 农药ID
    private String landParcelInfoId = ""; // 地块ID

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_pesticide);
        setTitle("农药使用");
    }

    @Override
    public void initMember() {

        getFertilizerList();
        getLandList();

        Calendar calendar = Calendar.getInstance();
        mYear = calendar.get(Calendar.YEAR);
        mMonth = calendar.get(Calendar.MONTH);
        mDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

        pvDate = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {
                tvDate.setText(year + "-" + (month + 1) + "-" + dayOfMonth);
            }
        }, mYear, mMonth, mDayOfMonth);

    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                switch (message.what) {
                    case MSG_S_0: // 保存
                        CustomDialog.nativeDialogFinish(activity, "保存成功");
                        break;
                    case MSG_F_0:
                        showToast(message.obj.toString());
                        break;
                    case MSG_S_1: // 农作物
                        callBack(message.obj);
                        break;
                    case MSG_S_2: // 地块
                        setLand(message.obj);
                        break;
                }
                return false;
            }
        });
    }

    private void setLand(Object obj) {

        final List<LandVO> landList = FastJsonUtil.getListBean(obj.toString(), "landParcelInfoVOList", LandVO.class);


        pvLand = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {

                tvLand.setText(landList.get(options1).getPickerViewText());
                landParcelInfoId = landList.get(options1).getLandParcelId();
            }
        })
                .setTitleText("地块")
                .setSubCalSize(16)//确定取消按钮大小
                .setLineSpacingMultiplier(1.5f) //设置两横线之间的间隔倍数（范围：1.2 - 2.0倍 文字高度）
                .setDividerColor(getResources().getColor(R.color.colorFontLightGrayB))//设置分割线的颜色
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(18)//设置滚轮文字大小
                .setSelectOptions(0)  //设置默认选中项
                .build();
        pvLand.setPicker(landList);

    }

    @Override
    public void callBack(Object object) {
        // 农药
        final List<FertilizerVO> pesticidesList = FastJsonUtil.getListBean(object.toString(), "pesticidesList", FertilizerVO.class);
        // 农药
        pvName = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {

                if (pesticidesList == null || pesticidesList.size() == 0){
                    showToast("正在获取农药，请稍候重试");
                    return;
                }

                tvName.setText(pesticidesList.get(options1).getPickerViewText());
                pesticidesStandardId = pesticidesList.get(options1).getId();
                tvDosage.setText("药量（" + pesticidesList.get(options1).getUnitMin() + "）");
            }
        })
                .setTitleText("农药")
                .setSubCalSize(16)//确定取消按钮大小
                .setLineSpacingMultiplier(1.5f) //设置两横线之间的间隔倍数（范围：1.2 - 2.0倍 文字高度）
                .setDividerColor(getResources().getColor(R.color.colorFontLightGrayB))//设置分割线的颜色
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(18)//设置滚轮文字大小
                .setSelectOptions(0)  //设置默认选中项
                .build();
        pvName.setPicker(pesticidesList);
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.layout_date, R.id.layout_name, R.id.layout_land, R.id.bt_save})
    public void onViewClicked(View view) {
        hideKeyboard();
        switch (view.getId()) {
            case R.id.layout_date:
                pvDate.show();
                break;
            case R.id.layout_name:
                if (pvName != null)
                    pvName.show();
                else {
                    showToast("正在获取农药");
                }
                break;
            case R.id.layout_land:
                if (pvLand != null)
                    pvLand.show();
                else {
                    showToast("正在获取地块");
                }
                break;
            case R.id.bt_save:
                save();
                break;
        }
    }

    private void save() {

        if (StringUtil.isTextEmpty(tvDate)) {
            showToast("请选择日期");
            return;
        }

        if (StringUtil.isTextEmpty(tvLand)) {
            showToast("请选择地块");
            return;
        }

        if (StringUtil.isTextEmpty(tvName)) {
            showToast("请选择农药");
            return;
        }

        if (StringUtil.isTextEmpty(etNumArea)) {
            showToast("请填写亩数");
            return;
        }

        if (StringUtil.isTextEmpty(etDosage)) {
            showToast("请填写药量");
            return;
        }

        if (StringUtil.isTextEmpty(etMember)) {
            showToast("请填写社员");
            return;
        }

        /**
         * userAccId	用户ID
         * dateTime	日期(2019-04-26)
         * landParcelInfoId	地块id
         * muNumber	亩数
         * pesticidesStandardId	农药id
         * dosage	人数
         * member	社员
         */
        HttpParams params = new HttpParams();
        params.put("enterpriseId", AdminXML.getEnterpriseId(activity));
        params.put("resNo", AdminXML.getResNo(activity));
        params.put("userAccId", AdminXML.getUserAccId(activity));
        params.put("dateTime", tvDate.getText().toString());
        params.put("landParcelInfoId", landParcelInfoId);
        params.put("pesticidesStandardId", pesticidesStandardId);
        params.put("muNumber", etNumArea.getText().toString());
        params.put("dosage", etDosage.getText().toString());
        params.put("member", etMember.getText().toString());

        OkHttpUtil.post(activity, URL.URL_SAVE_PESTICIDE_USE, "正在保存", params, mHandler,
                MSG_S_0, MSG_F_0);

    }

    // 获取地块
    private void getLandList() {
        HttpParams params = new HttpParams();
        params.put("enterpriseId", AdminXML.getEnterpriseId(activity));
        params.put("resNo", AdminXML.getResNo(activity));

        OkHttpUtil.post(activity, URL.URL_GET_LAND_LIST, "", params, mHandler,
                MSG_S_2, MSG_F_2);
    }

    // 农药 化肥  type = 0 化肥 type =1 农药 不传则取两种
    private void getFertilizerList() {
        HttpParams params = new HttpParams();
        params.put("enterpriseId", AdminXML.getEnterpriseId(activity));
        params.put("resNo", AdminXML.getResNo(activity));
        params.put("type", "1");


        OkHttpUtil.post(activity, URL.URL_GET_FERTILIZER_LIST, "", params, mHandler,
                MSG_S_1, MSG_F_1);
    }


}
